<template>
  <el-dialog :title="title"
             class="menu-edit-body"
             :visible.sync="dialogVisible"
             :destroy-on-close="true"
             top="80px"
             width="950px"
             :close-on-click-modal="false">
    <el-form :model="model"
             :rules="rules"
             style="text-align: left; width: 850px"
             label-width="110px"
             ref="form">
      <el-form-item prop="name"
                    label="歌单名">
        <el-input v-model="model.name"
                  placeholder="请输入歌单名"></el-input>
      </el-form-item>
      <el-form-item prop="content"
                    label="歌单简介">
        <el-input v-model="model.content"
                  type="textarea"
                  rows="3"
                  placeholder="请输入歌单简介"></el-input>
      </el-form-item>
      <el-form-item label="标签">
        <tag-select v-model="model.tags"></tag-select>
      </el-form-item>
      <el-form-item label="状态">
        <el-checkbox v-model="model.status"
                     :true-label="1"
                     :false-label="0">显示</el-checkbox>
      </el-form-item>
      <el-form-item label="歌曲"
                    prop="songs">
        <song-item-list ref="songItemList"
                        v-model="model.songs"></song-item-list>
      </el-form-item>
      <el-form-item label="封面图"
                    prop="cover">
        <image-input type="other"
                     v-model="model.cover"></image-input>
      </el-form-item>
      <el-form-item>
        <el-checkbox v-model="model.is_default"
                     :true-label="1"
                     :false-label="0">设为默认</el-checkbox>
      </el-form-item>
    </el-form>
    <div slot="footer"
         style="display: flex;justify-content: center;">
      <el-button type="primary"
                 @click="cancel">取消
      </el-button>
      <el-button type="primary"
                 @click="submit">保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import songItemList from '@/views/music/song/components/song-item-list'
import imageInput from '@/components/image-input'
import tagSelect from '@/components/common/tag-select'
import { store, edit, fetchView } from '../../api'

export default {
  data () {
    return {
      dialogVisible: false,
      refresh: false,
      id: 'add',
      model: {
        name: '',
        content: '',
        cover: [],
        tags: [],
        songs: [],
        status: 1,
        is_default: 0
      },
      rules: {
        name: [
          { required: true, message: '歌单名不能为空' },
          { max: 50, message: '最长50个字' }
        ],
        songs: [
          { required: true, message: '请选择歌曲', trigger: 'submit' },
        ],
        cover: [
          { required: true, message: '请选择封面图', trigger: 'submit' },
        ],
        content: [
          { max: 255, message: '最长255个字' }
        ],
      }
    }
  },
  computed: {
    title () {
      return this.isAdd ? '新增歌单' : '编辑歌单'
    },
    isAdd () {
      return this.id === 'add'
    }
  },
  components: {
    songItemList, imageInput, tagSelect
  },
  methods: {
    async open (id = false) {
      if (id !== false) {
        this.id = id;
        const { data: { data } } = await fetchView(this.id)
        const { cover, ...r } = data
        Object.assign(this.model, { cover: [cover], ...r })
      } else {
        this.initParams()
      }
      this.dialogVisible = true
      this.fetchSongList()
      return new Promise((resolve) => {
        this._resolve = resolve
      })
    },
    initParams () {
      Object.assign(this.model, {
        name: '',
        content: '',
        cover: [],
        tags: [],
        songs: [],
        status: 1,
        is_default: 0
      })
      this.id = 'add'
      this.refresh = false
    },
    cancel () {
      this._resolve({ refresh: this.refresh })
      this.dialogVisible = false
    },
    async submit () {
      await this.$refs.form.validate()
      const { cover: [{ id, url }], songs, ...r } = this.model
      const songList = songs.map(({ id }) => id)
      const params = { songs: songList, cover: { id, url }, ...r }
      if (this.isAdd) {
        await store(params)
      } else {
        await edit(this.id, params)
      }
      this.refresh = true
      this.cancel()
    },
    fetchSongList () {
      if (this.$refs.songItemList) {
        this.$refs.songItemList.query.keyword = undefined;
        this.$refs.songItemList.fetchList(1);
      } else {
        setTimeout(() => this.fetchSongList(), 200);
      }
    },
  }
}
</script>
